package com.jinmdz.app.api.api.service;

import com.jinmdz.app.api.api.model.user.JobNoData;
import com.jinmdz.app.api.base.BaseService;
import com.jinmdz.app.api.model.system.UserItem;
import com.jinmdz.app.api.wrapper.DictionaryWrapper;
import com.jinmdz.app.dao.dao.SystemUserDao;
import com.jinmdz.app.dao.model.dictionary.DataDictionaryItem;
import com.jinmdz.app.dao.model.user.SystemUserItem;
import com.jinmdz.fmis.common.ext.ArrayListExt;
import com.jinmdz.fmis.common.util.BeanUtil;
import com.jinmdz.fmis.common.util.DataUtil;
import com.jinmdz.fmis.common.util.StringUtil;
import com.jinmdz.fmis.core.base.BaseResult;
import com.jinmdz.fmis.core.constant.EDictCode;
import com.jinmdz.fmis.core.exception.ActionException;
import com.jinmdz.fmis.core.wrapper.ResultWrapper;
import com.jinmdz.fmis.mapper.entity.JobTaskEntity;
import com.jinmdz.fmis.mapper.mapper.JobTaskMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * JobTaskService
 *
 * @author LiCongLu
 * @date 2020-08-24 15:38
 */
@Service("jobTaskService")
public class JobTaskService extends BaseService {

    @Resource
    private SystemUserDao systemUserDao;

    @Resource
    private DictionaryWrapper dictionaryWrapper;

    @Resource
    private JobTaskMapper jobTaskMapper;

    /**
     * 执行员工任务记录标记
     * 返回员工名称信息，用于日志信息保存，举例：员工1,员工2
     *
     * @param userItem     当前账号
     * @param businessCode 业务编码
     * @param tableName    业务表名称
     * @param tableId      业务表主键
     * @param taskTypeCode 任务类型编码
     * @param userIds      员工主键
     * @return 员工账号信息
     * @author LiCongLu
     * @date 2020-08-24 15:44
     */
    public String saveJobTask(UserItem userItem, String businessCode, String tableName, Integer tableId
            , Integer taskTypeCode, ArrayListExt<Integer> userIds) throws ActionException {
        ArrayList<String> fullNames = new ArrayList<>();

        // 获取字典类型
        DataDictionaryItem dataDictionaryItem = dictionaryWrapper.getDataDictionaryItem(EDictCode.TASK_TYPE, taskTypeCode);
        if (DataUtil.isNull(dataDictionaryItem) || DataUtil.invalid(dataDictionaryItem.getDictCode())) {
            throw exception("员工任务编码错误，不存在此任务");
        }

        // 判断用户列表是否存在
        if (DataUtil.invalid(userIds)) {
            throw exception("员工主键不能为空");
        }

        // 遍历查询账号信息
        for (Integer userId : userIds) {
            SystemUserItem systemUserItem = systemUserDao.getSystemUserById(userId);
            if (DataUtil.isNull(systemUserItem) || DataUtil.invalid(systemUserItem.getJobNo())) {
                throw exception("员工主键存在错误，操作失败");
            }

            // 赋值任务内容
            JobTaskEntity entity = getEntity(userItem, JobTaskEntity.class);
            entity.setBusinessCode(businessCode)
                    .setUserId(userId)
                    .setTaskTypeCode(taskTypeCode)
                    .setTaskType(dataDictionaryItem.getDataText())
                    .setTableName(tableName)
                    .setTableId(tableId)
                    // 不可修改
                    .setUnmodified(1);
            // 添加工作任务
            jobTaskMapper.insertJobTask(entity);
            if (DataUtil.invalid(entity.getId())) {
                throw exception("记录员工工作任务失败");
            }

            fullNames.add(systemUserItem.getFullName());
        }

        return DataUtil.invalid(fullNames) ? "" : StringUtil.joinComma(fullNames);
    }
}
